package com.longqinuo.mycarservice;

import android.os.Bundle;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.longqinuo.mycarservice.fragments.AddGasFragment;
import com.longqinuo.mycarservice.fragments.FindFragment;
import com.longqinuo.mycarservice.fragments.HomeFragment;
import com.longqinuo.mycarservice.fragments.KeepServerFragment;
import com.longqinuo.mycarservice.fragments.MyCarsFragment;
import com.longqinuo.mycarservice.fragments.NearClearFragment;
import com.longqinuo.mycarservice.fragments.NewsListFragment;
import com.longqinuo.mycarservice.fragments.SecondCarFragment;
import com.longqinuo.mycarservice.fragments.UserFragment;
import com.longqinuo.mycarservice.utils.PathUtils;
import com.longqinuo.mycarservice.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements HomeFragment.OnHomeFragmentInteractionListener{

    private android.support.design.widget.TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mList;
    private FragmentManager mManager;
    private FragmentPagerAdapter mAdapter;
    private int mImages[] = {R.drawable.tab_home,R.drawable.tab_find,R.drawable.tab_user};
    private String mNames[] = {"首页","发现","我的"};

    private FragmentTransaction mTransaction;
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        initView();
        PathUtils.havedLogin = (boolean) SPUtils.getSp(this,"isLogin",false);
    }


    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_main_view_pager);
        mTabLayout = (android.support.design.widget.TabLayout) findViewById(R.id.id_main_tab_layout);
        initFragmentList();
        initConfig();
        overridePendingTransition(R.anim.launch_anim,R.anim.launch_anim_out);
    }

    private void initFragmentList() {
        mList = new ArrayList<>();
        mList.add(new HomeFragment());
        mList.add(new FindFragment());
        mList.add(new UserFragment());
    }

    private void initConfig() {
        mManager = getSupportFragmentManager();
        mAdapter = new FragmentPagerAdapter(mManager) {
            @Override
            public Fragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        };
        mViewPager.setAdapter(mAdapter);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorHeight(0);
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(getView(i));
        }
        mTabLayout.getTabAt(0).getCustomView().setSelected(true);


    }
    private View getView(int index) {
        View view = View.inflate(this,R.layout.tab_item,null);
        ImageView header = (ImageView) view.findViewById(R.id.iv_tab_item_header);
        TextView name = (TextView) view.findViewById(R.id.tv_tab_item_title);
        header.setImageResource(mImages[index]);
        name.setText(mNames[index]);
        return view;
    }

    @Override
    public void onHomeFragmentInteraction(View view) {

        mTransaction = mManager.beginTransaction();
        switch (view.getId()) {
            case R.id.home_break_query:  //违章查询 跳转
                if (PathUtils.havedLogin) {
                    mTransaction.add(R.id.ll_main_layout,new MyCarsFragment(),"MyCars");
                    mTransaction.addToBackStack("MyCars");

                } else {
                    Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.home_add_gas:     //加油站 跳转
                mTransaction.add(R.id.ll_main_layout,new AddGasFragment(),"AddGas");
                mTransaction.addToBackStack("AddGas");
                break;
            case R.id.home_near_clear:      //附近洗车 跳转
                mTransaction.add(R.id.ll_main_layout,new NearClearFragment(),"NearClear");
                mTransaction.addToBackStack("NearClear");
                break;
            case R.id.home_high_speed:    //高速路况 跳转
                Toast.makeText(this, "高速路况", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_second_hand_home:     //二手车 跳转
                mTransaction.add(R.id.ll_main_layout,new SecondCarFragment(),"SecondCar");
                mTransaction.addToBackStack("SecondCar");
                break;
            case R.id.ll_keep_server_home:      //维修保养 跳转
                mTransaction.add(R.id.ll_main_layout,new KeepServerFragment(),"KeepServer");
                mTransaction.addToBackStack("KeepServer");
                break;
            case R.id.tv_more_news_list:   //更多新闻列表 跳转
                mTransaction.add(R.id.ll_main_layout,new NewsListFragment(),"NewsList");
                mTransaction.addToBackStack("NewsList");
                break;
        }
        mTransaction.commit();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //两秒之内按返回键就会退出
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
